﻿#include "SMTExtFuncPageUI.h"
#include <QtWidgets>
#include "SMTGUI.h"
#include "SMTData.h"

SMTExtFuncPageUI::SMTExtFuncPageUI(QWidget *parent)
	:SMTSettingPage(parent),
	ui(new Ui::SMTExtFuncPageUIClass)
{
	ui->setupUi(this);

}

SMTExtFuncPageUI::~SMTExtFuncPageUI()
{
}

SMTString SMTExtFuncPageUI::name()
{
	SMTString name = QObject::tr("general").toLocal8Bit().constData();
	return name;
}

QWidget * SMTExtFuncPageUI::widget()
{
	return dynamic_cast<QWidget *>(this);
}

//初始化界面
void SMTExtFuncPageUI::initGUI()
{
	simpleLog("simple_demo.log", "initGUI");
	ui->comboBox_SelectPoint->addItem("UnSelect");
	QString SelectPointName;

	for (int i = 0; i < m_availableRef.length(); i++)
	{
		SMTVariable variable;
		m_availableRef.select(i, &variable);
		if (!variable.valid() || !variable.enabled() || !variable.isInUse())
		{
			continue;
		}
		SMTOperator* opr = variable.parent();
		QString strname;
		if (opr)
		{
			strname = opr->secName().toUtf8().constData();
			strname = strname + "-" + opr->name().toUtf8().constData() + ".";
			simpleLog("simple_demo.log", "strname:" + strname);
		}

		if (variable.variableType() == SMTVariable::VARIABLE_POINT)
		{
			simpleLog("simple_demo.log", "Point Ready:" + strname);
			QString itemname = strname + variable.name().toUtf8().constData();
			ui->comboBox_SelectPoint->addItem(itemname);
			simpleLog("simple_demo.log", "addItem:" + itemname);
		}
	}

	HandleInitUIChange(0, &SelectPointName);
	ui->comboBox_SelectPoint->setCurrentText(SelectPointName);
	HandleInitUIChange(1, ui->doubleSpinOffsetX);
	HandleInitUIChange(2, ui->doubleSpinOffsetY);

	connect(ui->comboBox_SelectPoint, SIGNAL(currentIndexChanged(int)),
		this, SLOT(on_comboBox_SelectPoint_valueChanged(int)));
}


//写入日志
void SMTExtFuncPageUI::simpleLog(QString fileName, QString body)
{
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");

	QDir *TEST = new QDir;
	bool exist = TEST->exists("log");
	if (!exist)
		bool ok = TEST->mkdir("log");
	QString fileName1 = "log/" + fileName;

	QFile file(fileName1);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
	{
		qDebug() << "open fail!" << endl;
	}
	QTextStream in(&file);
	current_date += ":" + body;
	in << current_date << "\n";
	file.close();
	delete TEST;
}


void SMTExtFuncPageUI::updateGUI()
{

}

//确认按钮按下
void SMTExtFuncPageUI::on_pushButton_clicked()
{
	double offsetX = ui->doubleSpinOffsetX->value();
	double offsetY = ui->doubleSpinOffsetY->value();

	SMTVariableArray *inParams;
	m_opr->GetInputVariables(&inParams);

	SMTNumber offsetx;
	offsetx.fromDouble(offsetX);

	SMTNumber offsety;
	offsety.fromDouble(offsetY);

	(*inParams)[1].setValue(offsetx);
	(*inParams)[2].setValue(offsety);

	//m_opr->run();
}

//下拉事件改变
void SMTExtFuncPageUI::on_comboBox_SelectPoint_valueChanged(int dvalue)
{
	if (dvalue != 0)
	{
		int index = 0;
		QString CurrentChoice = ui->comboBox_SelectPoint->currentText();
		QStringList variablename = CurrentChoice.split(".");

		if (variablename.length() != 0)
		{
			CurrentChoice = variablename[variablename.length() - 1];
			HandleComBoxValueChange(index, CurrentChoice, &m_availableRef, SMTRefFormat::PToP());
		}
	}
}

//初始化图像控件，获取引用变量
void SMTExtFuncPageUI::HandleInitUIChange(int index, QString *result)
{
	SMTVariableArray *inParams;
	m_opr->GetInputVariables(&inParams);
	if (m_opr)
	{
		SMTVariable variable;
		SMTRefFormat refFormat = SMTRefFormat::SToS();
		bool test = (*inParams)[index].getReferenceInfo(0, variable, &refFormat);
		QString variantName = variable.name().toLocal8Bit().constData();
		for (int i = 0; i < m_availableRef.length(); i++)
		{
			SMTVariable var;
			m_availableRef.select(i, &var);
			if (variantName == var.name().toLocal8Bit().constData())
			{
				*result = variantName;
			}
		}
	}

}

//QComBox设置所显示的值
void SMTExtFuncPageUI::HandleInitUIChange(int index, QDoubleSpinBox *pWidget)
{
	SMTVariableArray *inParams;
	m_opr->GetInputVariables(&inParams);
	SMTVariant val;
	(*inParams)[index].getValue(&val);
	double dvalue;
	val.toDouble(&dvalue);

	pWidget->blockSignals(true);
	pWidget->setValue(dvalue);
	pWidget->blockSignals(false);
}

//Combox修改变量值
void SMTExtFuncPageUI::HandleComBoxValueChange(int index, QString value,
	SMTVariableArray *pVariableArray, const SMTRefFormat &format)
{
	SMTVariableArray *inParams;
	m_opr->GetInputVariables(&inParams);
	if (m_opr)
	{
		for (int i = 0; i < pVariableArray->length(); i++)
		{
			SMTVariable variable;
			pVariableArray->select(i, &variable);
			if (value == variable.name().toLocal8Bit().constData())
			{
				SMTVariant val;
				variable.getValue(&val);
				(*inParams)[index].clearReference();
				bool test = (*inParams)[index].addReference(variable, format);
				(*inParams)[index].setUseReference(true);

			}
		}
	}
}
